package com.rayo.accesscontrollercopy.module.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rayo.accesscontrollercopy.R;
import com.rayo.accesscontrollercopy.config.GlobalVariables;
import com.rayo.accesscontrollercopy.model.db.bean.CurrentUserData;
import com.rayo.accesscontrollercopy.utils.TimeFormatUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lyc-pc on 17/7/10.
 */

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserViewHolder> {
    private List<CurrentUserData> mList;
    private Context mContext;
    private OnClicker mOnClicker;

    public OnClicker getOnClicker() {
        return mOnClicker;
    }

    public void setOnClicker(OnClicker onClicker) {
        mOnClicker = onClicker;
    }

    public UserAdapter(List<CurrentUserData> list, Context context) {
        mList = list;
        mContext = context;
    }

    @Override
    public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new UserViewHolder(LayoutInflater.from(mContext).inflate(R.layout.adapter_user, parent, false));
    }

    @Override
    public void onBindViewHolder(UserViewHolder holder, final int position) {
        CurrentUserData currentUserData = mList.get(position);
        if (GlobalVariables.getInstance().getmSystemData().getNameFormat() == 0)
            holder.mTvAdapterUserUser.setText(mContext.getString(R.string.user)+currentUserData.getFname() + " " + currentUserData.getLname());
        else
            holder.mTvAdapterUserUser.setText(currentUserData.getLname() + " " + currentUserData.getFname());
        holder.mTvAdapterUserTagId.setText(mContext.getString(R.string.tag_id)+currentUserData.getTagid());
        holder.mTvAdapterUserTime.setText(mContext.getString(R.string.time)+":"+ TimeFormatUtil.showDateTime(mContext,currentUserData.getTime()));
        if(mOnClicker!=null) {
            holder.mTvAdapterUserUnbind.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   mOnClicker.click(v,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class UserViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_adapter_user_user)
        TextView mTvAdapterUserUser;
        @BindView(R.id.tv_adapter_user_tagId)
        TextView mTvAdapterUserTagId;
        @BindView(R.id.tv_adapter_user_time)
        TextView mTvAdapterUserTime;
        @BindView(R.id.tv_adapter_user_unbind)
        TextView mTvAdapterUserUnbind;

        public UserViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
